package com.xunk.scene.handler.weather;

import com.xunk.common.core.utils.SpringUtils;
import com.xunk.common.core.utils.StringUtils;
import com.xunk.common.redis.service.RedisService;
import com.xunk.scene.domain.SceneConditionEquipment;
import com.xunk.scene.domain.SceneConditionWeather;
import com.xunk.scene.domain.Weather;
import com.xunk.scene.handler.ConditionWrapper;
import com.xunk.scene.handler.DefaultConditionHandler;
import com.xunk.scene.handler.IConditionHandler;
import com.xunk.scene.handler.equipment.ConditionEquipmentLeaf;
import com.xunk.scene.service.WeatherService;
import com.xunk.scene.util.ConditionUtil;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.naming.OperationNotSupportedException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.scene.handler.weather
 * @ClassName: ConditionWeatherLeaf
 * @Author: tangguangyuan
 * @Description:天气条件判断
 * @Date: 2021/5/19 16:15
 * @Version: 1.0
 */
@Slf4j
@NoArgsConstructor
public class ConditionWeatherLeaf extends DefaultConditionHandler implements IConditionHandler, ConditionWrapper {
    private WeatherService weatherService = SpringUtils.getBean(WeatherService.class);

    SceneConditionWeather sceneConditionWeather;
    boolean flag = false;

    public ConditionWeatherLeaf(SceneConditionWeather sceneConditionWeather) {
        this.sceneConditionWeather = sceneConditionWeather;
    }

    /**
     * @param oneMatchFlag true满足一条即可，false 需全部满足
     * @return
     * @throws OperationNotSupportedException
     */
    @Override
    public boolean match(boolean oneMatchFlag) throws OperationNotSupportedException {
        Long adCode = sceneConditionWeather.getAdCode();
        Weather weather = weatherService.queryWeatherFromAmap(adCode);
        String weatherType = sceneConditionWeather.getWeatherType();
        String current = "";
        String target = sceneConditionWeather.getPropValue();
        String compareOperator = sceneConditionWeather.getCompare();

        //循环遍历fields 获取到与天气条件表中相匹配的天气属性
        Field[] fields = weather.getClass().getDeclaredFields();
        for (Field field :
                fields) {
            field.setAccessible(true);
            if (StringUtils.isNotEmpty(weatherType)&&field.getName().equals(weatherType)) {
                try {
                    current = field.get(weather).toString();
                    if (StringUtils.isEmpty(current))
                        log.error(String.format("未获取到区域代码:%d的天气属性：%s", sceneConditionWeather.getAdCode(), sceneConditionWeather.getWeatherType()));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        //风力 为 <= 3时进行特殊处理
        if(current.contains("≤")){
            current= current.substring(1);
        }
        //判断是否满足该天气条件
        flag = ConditionUtil.compare(current, target, compareOperator);
        return flag;
    }

    @Override
    public <T> IConditionHandler wrap(T o) {
        return new ConditionWeatherLeaf((SceneConditionWeather) o);
    }

    @Override
    public <T> List<IConditionHandler> wrap(List<T> objects) {
        Iterator<T> iterator = objects.iterator();
        List<IConditionHandler> list = new ArrayList<>();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            list.add(new ConditionWeatherLeaf((SceneConditionWeather) next));
        }
        return list;
    }
}
